package com.xsy.android.compose.wandroid.logic.base.repository

import android.accounts.NetworkErrorException
import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.xsy.android.compose.wandroid.R
import com.xsy.android.compose.wandroid.logic.model.BaseModel
import com.xsy.android.compose.wandroid.logic.model.ClassifyModel
import com.xsy.android.compose.wandroid.logic.model.PlayError
import com.xsy.android.compose.wandroid.logic.model.PlayLoading
import com.xsy.android.compose.wandroid.logic.model.PlayState
import com.xsy.android.compose.wandroid.logic.model.PlaySuccess
import com.xsy.android.compose.wandroid.logic.utils.NetworkUtils
import com.xsy.android.compose.wandroid.logic.utils.showToast

abstract class BaseArticleRepository(private val app: Application): BaseArticlePagingRepository() {
    suspend fun getTree(state: MutableLiveData<PlayState<List<ClassifyModel>>>) {
        state.postValue(PlayLoading)
        if(!NetworkUtils.isConnected(app)){
            showToast(app, app.getString(R.string.no_network))
            state.postValue(PlayError(NetworkErrorException(app.getString(R.string.no_network))))
            return
        }

        val tree = getArticleTree()
        if(tree.errorCode == 0){
            val projectList = tree.data
            state.postValue(PlaySuccess(projectList))
        }else {
            state.postValue(PlayError(NetworkErrorException(app.getString(R.string.no_network))))
        }
    }

    abstract suspend fun getArticleTree(): BaseModel<List<ClassifyModel>>
}